﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin;

[Area("Admin")]
[Authorize]
public class ContentCateController : Base
{
    private readonly LoginUtil _LoginUtil;
    private readonly ContentCateService _contentCateService;
    private readonly AdminUserGroupService _adminUserGroupService;
    private readonly LogService _logService;
    public ContentCateController(LoginUtil LoginUtil, ContentCateService contentCateService, AdminUserGroupService adminUserGroupService, LogService logService)
    {
        _LoginUtil = LoginUtil;
        _contentCateService = contentCateService;
        _adminUserGroupService = adminUserGroupService;
        _logService = logService;
    }


    /// <summary>
    /// 输出JSON 树形栏目列表 内容管理的菜单列表使用
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Menu_Content)]
    public async Task<IActionResult> GetTree()
    {
        UserInfo adminUser = await _LoginUtil.UserInfoAsync();
        List<ContentCate> cates;
        if (adminUser.Gid != 1)
        {
            int[] cateIdChannl = await _contentCateService.GetRoleChannlAsync(adminUser.DeptId);
            cates = await _contentCateService.CateRoleListAsync(cateIdChannl);
        }
        else
        {
            cates = await _contentCateService.GetAllAsync();
        }

        List<ViewContentCate> treeModels = new();
        ContentCategoryExtensions.IndentedCate(cates, treeModels);

        return Json(treeModels);
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Index)]
    public async Task<IActionResult> IndexAsync(int json = 0)
    {
        if (json == 1)
        {
            var pagedList = await _contentCateService.GetContentCates();
            return Json(new { code = 0, data = pagedList });
        }
        return View();
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    public async Task<IActionResult> AddOrEditAsync(int? id, int edit = 0, int pid = 0)
    {
        if (edit == 1)
        {
            var data = await _contentCateService.GetInfo(id);
            return Json(new { status = 1, data });
        }

        var contentCateData = await _contentCateService.GetContentCateListAsync(new() { Gid = 1 });
        ViewBag.contentCateData = contentCateData;
        ViewBag.id = id;
        ViewBag.pid = pid;
        return View();

    }


    [HttpPost]
    [XssFieldFilter]//防Xss
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    [Log(Name = "新增内容栏目")]
    public async Task<IActionResult> CreatePostAsync(EditContentCate cateModel)
    {
        if (ModelState.IsValid)
        {
            ContentCate cate = new()
            {
                Name = cateModel.Name,
                Pid = cateModel.Pid,
                Theme = cateModel.Theme,
                Ctheme = cateModel.Ctheme,
                Ico = cateModel.Ico,
                Type = cateModel.Type,
                Num = cateModel.Num,
                Sort = cateModel.Sort
            };
            await _contentCateService.InsertAsync(cate);
            return Json(new { status = 1, msg = "新增成功！" });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = error });
        }
    }


    [HttpPost, ActionName("EditPost")]
    [XssFieldFilter]//防Xss
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    [Log(Name = "修改内容栏目")]
    public async Task<IActionResult> EditPostAsync(EditContentCate cateModel, int id, int dyEdit)
    {

        if (ModelState.IsValid)
        {
            ContentCate contentCate = await _contentCateService.GetInfo(id);
            if (dyEdit == 1)
            {

                contentCate.Sort = cateModel.Sort;
                await _contentCateService.UpdateAsync(contentCate);
                return Json(new { status = 1, msg = "修改成功！" });
            }

            contentCate.Pid = cateModel.Pid;
            contentCate.Name = cateModel.Name;
            contentCate.Theme = cateModel.Theme;
            contentCate.Ctheme = cateModel.Ctheme;
            contentCate.Ico = cateModel.Ico;
            contentCate.Sort = cateModel.Sort;
            contentCate.Num = cateModel.Num;
            contentCate.Type = cateModel.Type;
            await _contentCateService.UpdateAsync(contentCate);
            return Json(new { status = 1, msg = " 修改栏目" + contentCate.Name + " id " + id });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = error });
        }
    }

    [HttpPost, ActionName("Delete")]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Delete)]
    [Log(Name = "删除内容栏目")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        if (await _contentCateService.DeleteByPriKeyAsync(id) > 0)
        {
            return Json(new { status = 1, msg = "删除栏目,Id： " + id });
        }
        else
            return Json(new { status = 0, msg = "删除失败！可能因为当前栏目下有内容！" });
    }

}
